#!/bin/bash
#author : merci

function add_ftpuser(){
	read -p "Please input your ftp username : "	x
	read -p "Please input your ftp password : "	y
	ftpuserdir="/var/mail/$x"
	if [ ! -f "$ftpuserdir" ]
	then
		mkdir -p /data/www/$x
		useradd -d /data/www/$x -s /sbin/nologin -M $x -g ftp
		echo "$x" >> /etc/vsftpd/chroot_list
		echo "$y" | passwd --stdin $x
		chmod -R 777 /data/www/$x
		/etc/init.d/vsftpd restart
		echo -e "Add user successfly ! \n your new account is $x, password is $y, your directory is /data/www/$x "
	else
		echo -e "Ftp user directory already exist !"
		exit 0
	fi
}	

function del_ftpuser(){
	read -p "Please input ftp username : " x
	ftpuserdir="/var/mail/$x"
	if [ ! -f "$ftpuserdir" ]
	then
		echo -e "Ftp user directory not exist !"
		exit 0
	else
		userdel -r $x >/dev/null 2>&1
		userlist=`cat /etc/vsftpd/chroot_list`
		echo "" > /etc/vsftpd/chroot_list
		for a in $userlist
		do
			if [ $a != "$x" ]
			then
				echo "$a" > /etc/vsftpd/chroot_list	
			else
				continue
			fi
		done
		echo -e "Delete ftp user successfly !"
	fi
}

function list_ftpuser(){
	userlist=`cat /etc/vsftpd/chroot_list`
	for a in $userlist
	do
		echo $a
	done
}

echo -e "1.Add vsftpd user \n2.Delete vsftpd user \n3.List all vsftpd user ";
read -p "Please choose id :" c
	case $c in 
		1 )
			add_ftpuser
			;;
		2 )
			del_ftpuser
			;;
		3 )
			list_ftpuser
			;;
		* )
			echo "Please choose correct"
			;;
	esac